package com.zhehekeji.opsys.api.webservice.server;

import javax.jws.WebService;

import org.apache.cxf.interceptor.InInterceptors;
import org.apache.cxf.interceptor.OutInterceptors;
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhehekeji.opsys.api.code.LuckyVoteResultCode;
import com.zhehekeji.opsys.api.resp.GetSigninExchangeListResp;
import com.zhehekeji.opsys.api.resp.GetSigninPictureListResp;
import com.zhehekeji.opsys.api.resp.GetSigninPictureResp;
import com.zhehekeji.opsys.api.resp.GetSigninPrizeDetailResp;
import com.zhehekeji.opsys.api.resp.GetSigninPrizeListResp;
import com.zhehekeji.opsys.api.resp.GetSigninProductDetailResp;
import com.zhehekeji.opsys.api.resp.GetSigninProductListResp;
import com.zhehekeji.opsys.api.resp.GetSigninRecordResp;
import com.zhehekeji.opsys.api.resp.LuckySigninResp;
import com.zhehekeji.opsys.api.service.manager.SigninManagerService;

/**
 * @ClassName SigninManagerWS
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-7-13   上午10:43:58
 */
@InInterceptors(classes={GZIPInInterceptor.class})
@OutInterceptors(classes={GZIPOutInterceptor.class})
@WebService(endpointInterface="com.zhehekeji.opsys.api.webservice.server.SigninManager",
name="signinManager",portName="signinManagerPort",serviceName="signinManagerWS")
public class SigninManagerWS implements SigninManager{
	private static final Logger log = LoggerFactory.getLogger(SigninManagerWS.class);
	@Autowired
	private SigninManagerService signinManagerService;
	@Override
	public GetSigninPictureResp getSigninPicture(String pictureCode) {
		
		GetSigninPictureResp resp = signinManagerService.getSigninPicture(pictureCode);
		return resp;
		
	}

	@Override
	public GetSigninPictureResp getLatestSigninPicture(String signinCode) {
		
		GetSigninPictureResp resp = signinManagerService.getLatestSigninPicture(signinCode);
		return resp;
		
	}
	
	@Override
	public GetSigninPictureListResp getSigninPictureList(String signinCode, int firstIndex, int maxResult) {
		
		GetSigninPictureListResp resp = signinManagerService.getSigninPictureList(signinCode, firstIndex, maxResult);
		return resp;
		
	}

	@Override
	public GetSigninPictureResp getSigninPictureByAboveTime(String signinCode, String aboveTime, int epgType) {
		
		GetSigninPictureResp resp = signinManagerService.getSigninPictureByAboveTime(signinCode, aboveTime, epgType);
		return  resp;
	}

	@Override
	public GetSigninPrizeListResp getSigninPrizeList(String signinCode) {
		
		GetSigninPrizeListResp resp = signinManagerService.getSigninPrizeList(signinCode);
		return resp;
	}

	@Override
	public GetSigninPrizeDetailResp getSigninPrizeDetail(String prizeCode) {

		GetSigninPrizeDetailResp resp = signinManagerService.getSigninPrizeDetail(prizeCode);
		return resp;
	}

	@Override
	public GetSigninProductListResp getSigninProductList(String signinCode) {
		
		GetSigninProductListResp resp = signinManagerService.getSigninProductList(signinCode);
		return resp;
	}

	@Override
	public GetSigninProductDetailResp getSigninProductDetail(String signinCode, String productID) {
		
		GetSigninProductDetailResp resp = signinManagerService.getSigninProductDetail(signinCode, productID);
		return resp;
	}

	@Override
	public LuckySigninResp getSigninUser(String signinCode, String userID) {
		
		LuckySigninResp resp = signinManagerService.getSigninUser(signinCode, userID);
		return resp;
	}

	@Override
	public GetSigninExchangeListResp getSigninExchangeList(String signinCode, String userID, String prizeCode,
                                                           int firstIndex, int maxResult) {
		
		GetSigninExchangeListResp resp = signinManagerService.getSigninExchangeList(signinCode, userID, prizeCode, firstIndex, maxResult);
		return resp;
	}

	@Override
	public LuckySigninResp initSigninUser(String signinCode, String userID, String productID) {
		
		LuckySigninResp resp = signinManagerService.initSigninUser(signinCode, userID, productID);
		return resp;
	}

	@Override
	public LuckySigninResp luckySignin(String signinCode, String userID, String productID, String signinTime, String randomStr) {
		LuckySigninResp resp = new LuckySigninResp();
		try{
			resp = signinManagerService.luckySignin(signinCode, userID, productID, signinTime, randomStr);
		}catch(Exception e1){
			//由javax.persistence.OptimisticLockException这里共享锁抛出的异常，再执行一次
			try{
				log.info(String.format("用户[%s]参与活动[%s]签到时异常，事务回滚，程序自动控制，再签一次！", userID, signinCode));
				resp = signinManagerService.luckySignin(signinCode, userID, productID, signinTime, randomStr);
			}catch(Exception e2){
				String errorDesc = String.format("用户[%s]参与活动[%s]签到时异常，事务回滚，签到失败！", userID, signinCode);
				resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
				resp.setResultDesc(errorDesc);
				log.error(errorDesc);
			}
		}
		
		return resp;
		
	}

	@Override
	public LuckySigninResp exchangePrize(String prizeCode, String userID) {
		
		LuckySigninResp resp = signinManagerService.exchangePrize(prizeCode, userID);
		return resp;
	}

	@Override
	public LuckySigninResp updateSigninUser(String signinCode, String userID, String mobileNo) {
		
		LuckySigninResp resp = signinManagerService.updateSigninUser(signinCode, userID, mobileNo);
		return resp;
	}

	@Override
	public GetSigninRecordResp getSigninRecord(String signinCode, String userID, String startDay, String endDay) {
		
		GetSigninRecordResp resp = signinManagerService.getSigninRecord(signinCode, userID, startDay, endDay);
		return resp;
		
	}

	@Override
	public GetSigninRecordResp getCurrentDaySigninRecord(String signinCode, String userID) {
		
		GetSigninRecordResp resp = signinManagerService.getCurrentDaySigninRecord(signinCode, userID);
		return resp;
	}

	@Override
	public GetSigninRecordResp getSigninRecordByProductID(String signinCode, String productID, String userID,
			String startDay, String endDay) {
		
		GetSigninRecordResp resp = signinManagerService.getSigninRecordByProductID(signinCode, productID, userID, startDay, endDay);
		return resp;
	}

	@Override
	public GetSigninRecordResp getCurrentDaySigninRecordByProductID(String signinCode, String productID, String userID) {

		GetSigninRecordResp resp = signinManagerService.getCurrentDaySigninRecordByProductID(signinCode, productID, userID);
		return resp;
	}
	
}

